﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DigitalAnts.User;
using BAL.Utils;
using BAL.Entities;
using BAL.Components;
using System.Data;

namespace DigitalAnts
{
    public partial class InnerPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                productListTable();
            }
        }

        public void productListTable()
        {
            int currentPage = 1;
            int pageSize = this.DataPager1.PageSize;
            if (Convert.ToInt32(Request["Page"]) > 0)
            {
                currentPage = Convert.ToInt32(Request["Page"]);
            }

            int startRow = (currentPage - 1) * pageSize;
            int endRow = startRow + pageSize;
            DataTable dt = clsProductBAL.doProductList();
            if (endRow > dt.Rows.Count)
            {
                endRow = dt.Rows.Count;
            }
            //DataTable data = dt;
            //data.Clear();
            //for (int i = startRow; i < endRow; i++)
            //{
            //    data.ImportRow(dt.Rows[i]);
            //}
            dt.DefaultView.Sort = "colInsertDate DESC";
            lvProductList.DataSource = dt;
            lvProductList.DataBind();
        }

        public void doUpdateCart(string cart)
        {
            if (HttpContext.Current.Session["Logged"] != null)
            {
                if (HttpContext.Current.Session["Logged"].Equals(true))
                {
                    clsUser obj = new clsUser();
                    obj.UserID = Convert.ToInt32(Session["UserID"]);
                    obj.Card = cart;
                    clsUserBAL.doUpdateUserCart(obj);
                }
            }
        }

        public void doUpdateFavorite(string favorite)
        {
            if (HttpContext.Current.Session["Logged"] != null)
            {
                if (HttpContext.Current.Session["Logged"].Equals(true))
                {
                    clsUser obj = new clsUser();
                    obj.UserID = Convert.ToInt32(Session["UserID"]);
                    obj.Favorite = favorite;
                    clsUserBAL.doUpdateUserFavorite(obj);
                }
            }
        }

        protected void lvProductList_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Cart")
            {
                int productID = Int32.Parse(e.CommandArgument.ToString());
                int maxItem = 100;
                if (HttpContext.Current.Session["Logged"] != null)
                {
                    if (HttpContext.Current.Session["Logged"].Equals(true))
                    {
                        maxItem = 500;
                    }
                }

                List<int> cart = (List<int>)Session["Cart"];
                if (cart.Count < maxItem)
                {
                    cart = clsCartAndFavorite.addNewID(cart, productID);
                    Session["Cart"] = cart;
                    doUpdateCart(clsCartAndFavorite.getStringID(cart));
                    Response.Redirect("~/User/UserCart.aspx");
                }
                else
                {
                    Response.Redirect(Request.Url.ToString());
                }
            }

            if (e.CommandName == "Favorite")
            {
                if (HttpContext.Current.Session["Logged"] != null)
                {
                    if (HttpContext.Current.Session["Logged"].Equals(true))
                    {
                        int productID = Int32.Parse(e.CommandArgument.ToString());
                        int maxItem = 500;


                        List<int> favorite = (List<int>)Session["Favorite"];
                        if (favorite.Count < maxItem)
                        {
                            favorite = clsCartAndFavorite.addNewID(favorite, productID);
                            Session["Favorite"] = favorite;
                            doUpdateFavorite(clsCartAndFavorite.getStringID(favorite));
                            Response.Redirect("~/User/FavoriteList.aspx");
                        }
                        else
                        {
                            Response.Redirect(Request.Url.ToString());
                        }
                    }
                    else
                    {
                        Response.Redirect("Please login to add new favorite list");
                    }
                }
                else
                {
                    Response.Redirect("Please login to add new favorite list");
                }
            }
        }

    }

}
